import { createApp, createVNode } from "vue";
import pinia from "./stores/index";
import { useStore } from "./stores/store";
import router from "./utils/router";
import ElementPlus from "element-plus";
import { ElMessage, ElMessageBox } from "element-plus";
import "element-plus/dist/index.css";
import * as Icons from "@element-plus/icons";
// import locale from "element-plus/dist/locale/zh-cn.mjs";
import CKEditor from "@ckeditor/ckeditor5-vue";
// import { Edit, Plus } from "@element-plus/icons";
//引入 nprogress
import NProgress from "nprogress";
import "nprogress/nprogress.css";
// import '@/assets/styles/xxx.scss'
import icoSvg from "./assets/icons/index.vue";
import i18n from "./i18n";
import App from "./App.vue";
// import { setupProdMockServer } from "./mock/mockProdServer";
// import { setupProdMockServer } from "../mock/mockProdServer";
import axios from "axios";

const app = createApp(App);
// app.use(store, key);
app.use(pinia);

app.use(router);

const store = useStore();
window.store = store;

app.use(i18n);
// 页面刷新时初始化语言
i18n.global.locale.value = store.appLocale;
// 注册 Element 设置 i18n
// app.use(ElementPlus, { locale });
app.use(ElementPlus, { i18n: i18n.global.t });
app.use(CKEditor);

// window.app = app;

const env = import.meta.env;
// 页面刷新时更新标题
document.title = env.VITE_APP_LOGIN_TITLE;
if (
  env.VITE_API_URL &&
  env.VITE_API_URL.indexOf("http://") == -1 &&
  env.VITE_API_URL.indexOf("https://") == -1
) {
  //  http://127.0.0.1:9000/summer-admin/#/login
  let url = window.location.href;
  let index = url.indexOf("/", 8);
  // 自动补全当前系统的域名或ip
  env.VITE_API_URL = url.substring(0, index);
  store.setAppUrl(env.VITE_API_URL);
  // console.log("main.ts", env.VITE_API_URL);
}

// 请求网络最新版本，如果大于本地版本，则强制刷新。
// chrome、firefox 测试可自动升级，不需要 reload()。
// 此方法解决程序运行中，没关闭浏览器的情况升级，提醒用户升级信息。
const isNewVersion = () => {
  let url =
    env.VITE_API_URL + `/summer-admin/version.json?t=` + new Date().getTime();
  axios.get(url).then((res) => {
    let version = res.data.version;
    if (env.VITE_APP_VER != version) {
      clearTimeout(isNewVersionTimer);
      ElMessageBox.alert(
        `检测到新版本：${res.data.version}，系统将自动刷新页面完成升级`,
        "提示信息",
        {
          type: "warning",
          confirmButtonText: "确定",
          callback: (action: any) => {
            window.location.reload();
          },
        }
      );
      return true;
    } else {
      return false;
    }
  });
};
console.log(`summer-admin-vue ver：${env.VITE_APP_VER}`);
const isNewVersionTimer = setInterval(() => {
  isNewVersion();
}, 1000 * 60 * 5);

// 进度条开始
router.beforeEach((to, from, next) => {
  // console.log("main.ts to", to);
  if (to.fullPath == "/login") {
    // 简单配置
    NProgress.inc(0.2);
    NProgress.configure({ easing: "ease", speed: 500, showSpinner: false });
    NProgress.start();
  }
  next();
});
// 进度条结束
router.afterEach(() => {
  NProgress.done();
});

// app.component("ico-edit", Edit);
// app.component("ico-plus", Plus);
// 创建Icon组件
const icoAuto = (props: { icon: string; size: number; color: string }) => {
  const { icon, size, color } = props;
  return createVNode(Icons[icon as keyof typeof Icons], {
    width: size,
    height: size,
    color: color,
  });
};

// setupProdMockServer();

// 注册Icon组件
app.component("ico-auto", icoAuto);
app.component("ico-svg", icoSvg);
app.mount("#app");
